<?php
class PressesController extends AppController {

	var $name = 'Presses';
	var $uses=array('Press','PaperSize');
	var $components = array('Auth','FileDelete');
	function beforeFilter() {		
		parent::beforeFilter();
		$this->layout="admin";
		$this->Auth->authorize = 'controller';
		// private method
		$this->Auth->deny('getPageSize');
	}
	function getPageSize($pageSize1,$pageSize2,$pageSizes=array()){
		$regex='/.* \((\\d{1,2})(?:-(\\d{1,2}))?cms\/(\\d{1,2})(?:-(\\d{1,2}))?"\)/';
		$pageSize1+='';
		$pageSize2+='';
		foreach($pageSizes as $pageSize){
			$name=$pageSize['PaperSize']['name'];
			if(($result=preg_match($regex,$name,$matches))){
				if($matches[2]==''){
					if($pageSize1==$matches[1]){
						if(!isset($matches[4])){
							if($pageSize2==$matches[3]){
								return $name;
							}
						}
						else{
							if($pageSize2<=$matches[4]&&$pageSize2>=$matches[3]){
								return $name;
							}
						}
					}
				}
				else{
					if($pageSize1>=$matches[2]&&$pageSize1<=$matches[1]){
						if(!isset($matches[4])){
							if($pageSize2==$matches[3]){
								return $name;
							}
						}
						else{
							if($pageSize2<=$matches[4]&&$pageSize2>=$matches[3]){
								return $name;
							}
						}						
					}
				}
			}
		}
		return "";
	}	
	function isAuthorized() {
		$MY_RULE_ID = 3; //Xem id tuong ung voi controller nay trong DB: table rules, vi du controller Staff/StaffTypes la 7.
		if ($this->action == 'login' || $this->action == 'logout') {//Cho phep su dung cac action ma khong can dang nhap        
            return true;
        }else{
			$allow = $this->requestAction('ruleStaffTypes/allow/'.$MY_RULE_ID);
			//Check xem co quyen su dung khong.
			if ($allow){
				return true;
			}else{
				$this->Auth->authError(__('Access denied!', true));
				$this->redirect(array('controller'=>'staffs','action'=>'login','admin'=>false));
				return false;
			}        
		}
		return false;
	}
	function admin_index($machineNumber=false,$manufacture=false,$sheetSize=false,$colours=false,$inStock=false){
		$this->paginate = array(
			'fields' => array('Press.id','Press.perfect','Press.name','Press.make', 'PressType.name','Press.year','Press.colour','Press.perfect','Press.paper_size1','Press.paper_size2'),
			'limit' => 10,
			'order' => array(
				'Press.modified' => 'desc'
			)
		);		
		$this->Press->recursive = 1;
		$conditions = null;
		// <--
		//Kiem tra neu co /make:2 , 2 la ma nha san xuat
        if (!empty($this->passedArgs['serial'])) {
             $conditions[]= array('Press.serial_number' => $this->passedArgs['serial']);			 
        }
        if (!empty($this->passedArgs['make'])) {
             $conditions[]= array('PressMake.name' => $this->passedArgs['make']);			 
        }
        if (!empty($this->passedArgs['size'])) {
             $conditions[]= array('CONCAT(Press.paper_size1,"x",Press.paper_size2)' => $this->passedArgs['size']);			 
        }
        if (!empty($this->passedArgs['special'])) {
             $conditions[]= array('Press.perfect' => $this->passedArgs['special']);			 
        }
        if (!empty($this->passedArgs['stock'])) {
             $conditions[]= array('Press.in_stock' => $this->passedArgs['stock']);
        }
		if (!empty($this->passedArgs['colour'])) {
             $conditions[]= array('Press.colour' => $this->passedArgs['colour']);
        }
		// -->
		//Them cac dieu kien da dat vao truy van
		$presses = $this->paginate('Press',$conditions);
		$pageSizes=$this->PaperSize->find('all',array('fields'=>array('name')));
		for($i=0;$i<count($presses);++$i){
			$currentPress=$presses[$i];
			if(($pagesize=$this->getPageSize($currentPress['Press']['paper_size1'],$currentPress['Press']['paper_size2'],$pageSizes))!="")
				$presses[$i]['Press']['paper_size']=$pagesize;
			else{
				$presses[$i]['Press']['paper_size']='not defined';
			}
		}
		$this->set('presses', $presses);		
		$this->set('paginates', $this->passedArgs);		
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid press', true));
			$this->redirect(array('action' => 'admin_index'));
		}
		$this->set('press', $this->Press->read(null, $id));
	}
	function admin_add() {
		$this->set("fileupload",date("YmdHis_").rand(100,999));
		$this->set("pathupload","presses");
		if (!empty($this->data)) {
			if($this->data['Press']['photo1']==""){
				$this->Session->setFlash(__('Please select image for press', true));				
				//delete unused file
				$savedfiles=array();
				for($i=1;$i<=4;$i++){
					if ($this->data['Press']['photo'.$i]!=null && $this->data['Press']['photo'.$i]!=""){
						$savedfiles[]=$this->data['Press']['photo'.$i];
					}else{
						break;
					}
				}
				$this->FileDelete->DeleteTmp($savedfiles,substr($this->data['Press']['photo1'],0,16));
			}
			else{
				$this->Press->create();
				if ($this->Press->save($this->data)) {
					$this->Session->setFlash(__('The press has been saved', true));
					$this->redirect(array('action' => 'admin_index'));					
				} else {
					$this->Session->setFlash(__('The press could not be saved. Please, try again.', true));
				}
			}
		}else{
			$this->Session->setFlash(null);
		}
		$statuses = $this->Press->Status->find('list');
		$pressMakes = $this->Press->PressMake->find('list');
		$pressModels = $this->Press->PressModel->find('list');
		$pressTypes = $this->Press->PressType->find('list');
		$sellTypes = $this->Press->SellType->find('list');
		$countries = $this->Press->Country->find('list');
		$conditions=array('1'=>'Poor', '2'=>'Below average','3'=>'Average','4'=>'Good','5'=>'Excellent');
		$this->set(compact('statuses', 'pressMakes', 'pressModels', 'pressTypes', 'sellTypes', 'countries','conditions'));		
	}

	function admin_edit($id = null) {
		$this->set("fileupload",date("YmdHis_").rand(100,999));
		$this->set("pathupload","presses");
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid press', true));
			$this->redirect(array('action' => 'admin_index'));
		}
		if (!empty($this->data)) {
			$this->Press->id=$this->data['Press']['id'];
			$fields=array('name','offered','sup_reference','serial_number','type','make','model','year','perfect','colour','paper_size1','paper_size2','photo1','photo2','photo3','photo4','video');
			// save data
			if ($this->Press->save($this->data,true,$fields)) {
				$this->Session->setFlash(__('The press has been saved', true));
				$this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('The press could not be saved. Please, try again.', true));
			}
		}
		if ($id){
			$this->data =$this->Press->read(null, $id);
			if(!$this->data){
				$this->Session->setFlash(__('The press not exit', true));
				$this->redirect(array('action' => 'admin_index'));			
			}
		}
		$statuses = $this->Press->Status->find('list');
		$pressMakes = $this->Press->PressMake->find('list');
		$pressModels = $this->Press->PressModel->find('list');
		$pressTypes = $this->Press->PressType->find('list');
		$sellTypes = $this->Press->SellType->find('list');
		$countries = $this->Press->Country->find('list');
		$conditions=array('1'=>'Poor', '2'=>'Below average','3'=>'Average','4'=>'Good','5'=>'Excellent');		
		$this->set(compact('statuses', 'pressMakes', 'pressModels', 'pressTypes', 'sellTypes', 'countries','conditions'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for press', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Press->delete($id)) {
			$this->Session->setFlash(__('Press deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Press was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	// edit status
	function admin_editstatus($id=null){
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid press', true));
			$this->redirect(array('action' => 'admin_index'));
		}
		if (!empty($this->data)) {			
			$this->Press->id=$this->data['Press']['id'];
			// save data
			$fields=array('name','status','buy_price','sell_price','sell_type_id','available_from_date','first_off_date','condition','active');
			if ($this->Press->save($this->data,true,$fields)) {
				$this->Session->setFlash(__('The press status has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The press status could not be saved. Please, try again.', true));
			}
		}
		else{
			$this->data =$this->Press->read(null, $id);
			if(!$this->data){
				$this->Session->setFlash(__('The press not exit', true));
				$this->redirect(array('action' => 'admin_index'));
			}
		}
		$statuses = $this->Press->Status->find('list');
		$sellTypes = $this->Press->SellType->find('list');
		$conditions=array('1'=>'Poor', '2'=>'Below average','3'=>'Average','4'=>'Good','5'=>'Excellent');
		$countries = $this->Press->Country->find('list');
		$this->set(compact('statuses', 'sellTypes', 'countries','conditions'));
	}
}
?>